package upDownLoad;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import tool.MD5Filter;
import dataStore.UserDataStoreApiInterface;
import dataStruct.FtpServerInfo;
import dataStruct.User;
import ftp.FtpApi;

public class UpDownLoad {
	public UpDownLoad(UserDataStoreApiInterface masterDSApi, User user) {
		UpDownLoad.masterDSApi = masterDSApi;
		UpDownLoad.user = user;
	}

	public Boolean upload_file(String localPath, String masterPath)
			throws IOException, SQLException {
		File file = new File(localPath);
		String file_md5 = MD5Filter.getMd5ByFile(file);
		if (masterDSApi.md5_exist(file_md5) == false) {
			// debug
			System.out.println("fileMd5  not exist\t(UpDownLoad.java)");
			masterDSApi.insert_file_info(file_md5, file.length());
			if (masterDSApi.insert_user_file(user.name, masterPath, file_md5) == false) {
				System.out.println("masterPath not exist  :" + masterPath);
				return false;
			}
			FtpServerInfo ftpServerInfo = masterDSApi.get_ftp_server_info();
			FtpApi ftpApi = new FtpApi();
			ftpApi.connect(ftpServerInfo.ftpServerAdress,
					ftpServerInfo.ftpServerPort, ftpServerInfo.ftpServerUser,
					ftpServerInfo.ftpServerPassword);
			String ftpFilePath = ftpServerInfo.ftpServerPath + "/" + file_md5;

			// debug
			System.out.println(String.format(
					"localPath = %s\t ftpFilePath=%s\t(UpDownLoad.java)",
					localPath, ftpFilePath));
			ftpApi.upload(localPath, ftpFilePath);
			
			masterDSApi.split_download(ftpFilePath);

			//ftpApi.delete(ftpFilePath);
			ftpApi.disconnect();
		} else {
			System.out.println("fileMd5  has exist");
			masterDSApi.update_userCount_fileInfo(file_md5, 1);
			masterDSApi.insert_user_file(user.name, masterPath, file_md5);
		}
		return true;
	}

	public Boolean download_file(String masterPath, String localPath) throws IOException, SQLException, InterruptedException {
		FtpServerInfo ftpServerInfo;
		if ((ftpServerInfo = masterDSApi.get_ftpServerInfo_by_Path(user.name, masterPath)) == null) {
			System.out.println("get ftpServerInfo error");
			return false;
		}

		FtpApi ftpApi = new FtpApi();
		ftpApi.connect(ftpServerInfo.ftpServerAdress,
				ftpServerInfo.ftpServerPort, ftpServerInfo.ftpServerUser,
				ftpServerInfo.ftpServerPassword);
		String ftpFilePath = ftpServerInfo.ftpServerPath;

		// debug
		System.out.println(String.format(
				" ftpFilePath=%s\tlocalPath = %s\t(UpDownLoad.java)",
				ftpFilePath, localPath));
		while(ftpApi.download(ftpFilePath, localPath) == false)
		{
			Thread.sleep(1000);
		}
		ftpApi.disconnect();
		return true;
	}
	
	public boolean map_reduce(String masterPath, String localPath) throws IOException, SQLException, InterruptedException {
		// TODO Auto-generated method stub
		
		FtpServerInfo ftpServerInfo;
		if ((ftpServerInfo = masterDSApi.wordcount_get_ftpServerInfo_by_Path(user.name, masterPath)) == null) {
			System.out.println("get ftpServerInfo error");
			return false;
		}

		if(masterDSApi.map(user.name, masterPath) == null)
		{
			System.out.println("map task error");
			return false;
		}
		FtpApi ftpApi = new FtpApi();
		ftpApi.connect(ftpServerInfo.ftpServerAdress,
				ftpServerInfo.ftpServerPort, ftpServerInfo.ftpServerUser,
				ftpServerInfo.ftpServerPassword);
		String ftpFilePath = ftpServerInfo.ftpServerPath;

		// debug
		System.out.println(String.format(
				" ftpFilePath=%s\tlocalPath = %s\t(UpDownLoad.java)",
				ftpFilePath, localPath));
		while(ftpApi.reduce(ftpFilePath + ".wordcount", localPath) == false)
		{
			Thread.sleep(1000);
		}
		//ftpApi.delete(ftpFilePath);
		ftpApi.disconnect();
		
		return true;
	}
	private static User user;
	private static UserDataStoreApiInterface masterDSApi;

}
